/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.pekall.myDictionary.screens;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

public class PanelSwitcher extends FrameLayout {
    // private static String TAG = PanelSwitcher.class.getCanonicalName();
    private static final int MAJOR_MOVE = 60;

    private static final int ANIM_DURATION = 200;

    private GestureDetector mGestureDetector;

    private int mCurrentView;

    private View children[];

    private int mWidth;

    private TranslateAnimation inLeft;

    private TranslateAnimation outLeft;

    private TranslateAnimation inRight;

    private TranslateAnimation outRight;

    public PanelSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCurrentView = 0;
        mGestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                            float velocityY) {
                        int dx = (int) (e2.getX() - e1.getX());
                        if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {

                            if (velocityX > 0) {
                                moveRight();
                            } else {
                                moveLeft();
                            }
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
    }

    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH) {
        mWidth = w;
        inLeft = new TranslateAnimation(mWidth, 0, 0, 0);
        outLeft = new TranslateAnimation(0, -mWidth, 0, 0);
        inRight = new TranslateAnimation(-mWidth, 0, 0, 0);
        outRight = new TranslateAnimation(0, mWidth, 0, 0);
        inLeft.setDuration(ANIM_DURATION);
        outLeft.setDuration(ANIM_DURATION);
        inRight.setDuration(ANIM_DURATION);
        outRight.setDuration(ANIM_DURATION);
    }

    public void onFinishInflate() {

        int count = getChildCount();
        children = new View[count];
        for (int i = 0; i < count; ++i) {
            children[i] = getChildAt(i);
            if (i != mCurrentView) {
                children[i].setVisibility(View.GONE);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    void moveLeft() {

        if (children.length < 2)
            return;

        if (mCurrentView < children.length - 1) {
            mOnMoveNotifyListener.onMoveBegin(mCurrentView + 1);

            children[mCurrentView + 1].setVisibility(View.VISIBLE);
            children[mCurrentView + 1].startAnimation(inLeft);
            children[mCurrentView].startAnimation(outLeft);
            children[mCurrentView].setVisibility(View.INVISIBLE);

            mCurrentView++;
            mOnMoveNotifyListener.onMoveEnd(mCurrentView);
        } else {
            mOnMoveNotifyListener.onMoveBegin(0);

            children[0].setVisibility(View.VISIBLE);
            children[0].startAnimation(inLeft);
            children[mCurrentView].startAnimation(outLeft);
            children[mCurrentView].setVisibility(View.INVISIBLE);

            mCurrentView = 0;
            mOnMoveNotifyListener.onMoveEnd(mCurrentView);
        }
    }

    void moveRight() {

        if (children.length < 2)
            return;

        if (mCurrentView > 0) {
            mOnMoveNotifyListener.onMoveBegin(mCurrentView - 1);

            children[mCurrentView - 1].setVisibility(View.VISIBLE);
            children[mCurrentView - 1].startAnimation(inRight);
            children[mCurrentView].startAnimation(outRight);
            children[mCurrentView].setVisibility(View.INVISIBLE);

            mCurrentView--;
            mOnMoveNotifyListener.onMoveEnd(mCurrentView);
        } else {
            mOnMoveNotifyListener.onMoveBegin(children.length - 1);

            children[children.length - 1].setVisibility(View.VISIBLE);
            children[children.length - 1].startAnimation(inRight);
            children[mCurrentView].startAnimation(outRight);
            children[mCurrentView].setVisibility(View.INVISIBLE);

            mCurrentView = children.length - 1;
            mOnMoveNotifyListener.onMoveEnd(mCurrentView);
        }
    }

    public View getView(int mCurrentView) {
        return children[mCurrentView];
    }

    int getCurrentIndex() {
        return mCurrentView;
    }

    public void setOnMoveNotifyListener(OnMoveNotifyListener listener) {
        mOnMoveNotifyListener = listener;
    }

    public interface OnMoveNotifyListener {
        void onMoveBegin(int mCurrentView);

        void onMoveEnd(int mCurrentView);
    }

    private OnMoveNotifyListener mOnMoveNotifyListener = null;
}
